package com.zkx.service;

import com.zkx.bean.User;
import com.zkx.test.Testdemo1;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zkx
 */
public class UserService {


    /**
     * 添加员工
     */
    public void addUser(Map<Integer,User> users, User user) throws IOException {
        users.put(user.getId(),user);
        //再序列化到文件中
        this.saveUser(users);
    }

    /**
     * 反序列化得员工
     */
    public Map<Integer,User> getUser() throws IOException, ClassNotFoundException {
        String filename = "D:\\userInfo.txt";
        //对象流 反序列化
        ObjectInputStream ois =
                new ObjectInputStream(new FileInputStream(filename));
        Map<Integer,User> fileusers = new HashMap<Integer,User>();
        Object obj = ois.readObject();
        fileusers = (Map<Integer,User>) obj;
        ois.close();
        return fileusers;

    }
    /**
     * 序列化得到员工
     */
    public void saveUser(Map<Integer,User> users) throws IOException {

        String filename = "D:\\userInfo.txt";
        //对象流
        ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream(filename));
        //文件，输入流，输入到文件
        oos.writeObject(users);
        //关闭流
        oos.close();
    }

}
